home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / snip9_91.arc / FIGETS.C < prev    next >
C/C++ Source or Header  |  1991-09-17  |  1KB  |  43 lines

  1. /*
  2. ** figets like fgets only works backward from filepos pos instead of
  3. ** forward from the current filepointer
  4. **
  5. ** returns fileposition of the begin of line read
  6. **
  7. ** by Jan Vroonhof
  8. */
  9.  
  10. #define MAXLEN 90
  11.  
  12. long pascal figets(FILE *file,char *buffer,long pos)
  13. {
  14.       char *i;
  15.       long aap;
  16.  
  17.       aap = (pos - MAXLEN > 0 ? pos-MAXLEN : 0L);
  18.       fseek(file, aap, SEEK_SET);
  19.       fread(buffer + 100, 1, MAXLEN, file);
  20.       buffer[pos - aap + 100] = 0;
  21.       i = strrchr(buffer + 100, '\n');
  22.       if (i)
  23.       {
  24.             *i = 0;
  25.             i = strrchr(buffer + 100, '\n');
  26.             if (i)
  27.             {
  28.                   strcpy(buffer, i + 1);
  29.                   return aap + (i -  buffer - 100) + 1;
  30.             }
  31.             else
  32.             {
  33.                   strcpy(buffer, buffer + 100);
  34.                   return (aap ? -1L : 0L);
  35.             }
  36.       }
  37.       else
  38.       {
  39.             strcpy(buffer, buffer + 100);
  40.             return -1L;
  41.       }
  42. }
  43.